using System;
using System.Drawing;
using System.Windows.Forms;

namespace DotDiff.TestInterface.Controls
{

    /// <summary>
    /// Numbered Textbox, orignal can be found here http://www.codeproject.com/KB/edit/numberedtextbox.aspx
    /// There are some alterations in this copy. I cannot take create for the Orignal work :)
    /// </summary>
    public partial class NumberedTextBoxUC : UserControl
    {

        private Font defaultFont; 

        public NumberedTextBoxUC()
        {
            InitializeComponent();
            defaultFont = numberLabel.Font;
            //numberLabel.Font = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size + 1.019f);
        }


        private void updateNumberLabel()
        {
            //we get index of first visible char and number of first visible line
            Point pos = new Point(0, 0);
            int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
            int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);

            //now we get index of last visible char and number of last visible line
            pos.X = ClientRectangle.Width;
            pos.Y = ClientRectangle.Height;
            int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);
            int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);

            //this is point position of last visible char, we'll use its Y value for calculating numberLabel size
            pos = richTextBox1.GetPositionFromCharIndex(lastIndex);


           
            //finally, renumber label
            numberLabel.Text = "";
            for (int i = firstLine; i <= lastLine + 1; i++)
            {
                numberLabel.Text += (i + 1).ToString() +Environment.NewLine;
            }

        }


        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            //richTextBox1.SelectAll();
            //richTextBox1.SelectionFont = defaultFont;
            //richTextBox1.Font = defaultFont;
            //richTextBox1.DeselectAll();
            //richTextBox1.BorderStyle = BorderStyle.None;
            //richTextBox1.Margin = Top = 0;
            updateNumberLabel();
        }

        private void richTextBox1_VScroll(object sender, EventArgs e)
        {
            //move location of numberLabel for amount of pixels caused by scrollbar
            int d = richTextBox1.GetPositionFromCharIndex(0).Y % (richTextBox1.Font.Height + 1);
            
            numberLabel.Location = new Point(0, d);

            updateNumberLabel();
        }

        private void richTextBox1_Resize(object sender, EventArgs e)
        {
            richTextBox1_VScroll(null, null);
        }

        private void richTextBox1_FontChanged(object sender, EventArgs e)
        {
            updateNumberLabel();
            richTextBox1_VScroll(null, null);
        }

             
        /// <summary>
        /// referecne to the rich text box.
        /// </summary>
        public RichTextBox TextBox
        {
            get
            {
                return richTextBox1;
            }
        }

        public override string Text
        {
            get
            {
                return richTextBox1.Text;
            }
            set
            {
                richTextBox1.Text = value;
            }
        }

        private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
        {

        }

    }
}